//
//  NSString+urlstring.m
//  helper
//
//  Created by huangyi on 12-10-29.
//  Copyright (c) 2012年 huangyi. All rights reserved.
//

#import "NSString+urlstring.h"
#import "NSString+encode.h"
#import "NSDate+timestamp.h"

@implementation NSString (urlstring)

-(NSDate*)rfc822date{
    return [NSDate dateFromRFC822String:self];
}
-(NSDate*)rfc3339date{
    return [NSDate dateFromRFC3339String:self];
}

-(NSString*)fileURLString{
    NSURL *url=[NSURL fileURLWithPath:self];
    return url.absoluteString;
}

-(NSDictionary*)HTTP_HEAD{
    NSURL *url=[NSURL URLWithString:self];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod=@"HEAD";
    request.cachePolicy=NSURLRequestReloadIgnoringLocalCacheData;
    NSHTTPURLResponse *response=nil;
    NSError *error=nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error) {
        NSLog(@"%@",error.description);
        return nil;
    }else{
        if (response.statusCode==200) {
            return response.allHeaderFields;
        }else{
            return nil;
        }
    }
}

-(NSData*)HTTP_GET:(NSTimeInterval)timeout{
    NSURL *url=[NSURL URLWithString:self];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod=@"GET";
    request.cachePolicy=NSURLRequestReloadIgnoringLocalCacheData;
    if (timeout) {
        request.timeoutInterval=timeout;
    }
    NSHTTPURLResponse *response=nil;
    NSError *error=nil;
    NSData *result=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error) {
        NSLog(@"%@",error.description);
        return nil;
    }else{
        if (response.statusCode==200) {
            if (result) {
                return [NSData dataWithData:result];
            }else{
                return nil;
            }
        }else{
            return nil;
        }
    }
}

-(NSData*)HTTP_POST:(NSData*)data timeout:(NSTimeInterval)timeout{
    NSURL *url=[NSURL URLWithString:self];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod=@"POST";
    request.cachePolicy=NSURLRequestReloadIgnoringLocalCacheData;
    if (data) {
        request.HTTPBody=data;
    }
    if (timeout) {
        request.timeoutInterval=timeout;
    }
    NSHTTPURLResponse *response=nil;
    NSError *error=nil;
    NSData *result=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if (error) {
        NSLog(@"%@",error.description);
        return nil;
    }else{
        if (response.statusCode==200) {
            if (result) {
                return [NSData dataWithData:result];
            }else{
                return nil;
            }
        }else{
            return nil;
        }
    }
}

-(NSData*)HTTP_GET{
    return [self HTTP_GET:0];
}
-(NSData*)HTTP_POST:(NSData*)data{
    return [self HTTP_POST:data timeout:0];
}

@end

NSURL* NSURLObj(NSString *urlstr){
    NSURL *url=[NSURL URLWithString:urlstr];
    if (url==nil) {
        url=[NSURL URLWithString:urlstr.urlencode];
    }
    return url;
}

